﻿using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace CPPEI.Coolzon.Designer.Views
{
    public partial class DesignView
    {
        /// <summary>
        /// 根据frame的大小和显示的场景大小来设置滚动条的值
        /// </summary>
        private void SetScrollBar()
        {
            if (this._ctlMainFrame.CurrentScene == null)
            {
                return;
            }
            double contentWidth = this._ctlMainFrame.CurrentScene.XWidth;
            double contentHeight = this._ctlMainFrame.CurrentScene.XHeight;

            double containerWidth = this._ctlMainFrame.ActualWidth / this._ctlMainFrame.ZoomFactor;
            double containerHeight = this._ctlMainFrame.ActualHeight / this._ctlMainFrame.ZoomFactor;

            if (contentWidth > containerWidth)
            {
                _ctlScrollBarH.Maximum = contentWidth - containerWidth;
                _ctlScrollBarH.IsEnabled = true;
                _ctlScrollBarH.ViewportSize = containerWidth;
            }
            else
            {
                _ctlScrollBarH.IsEnabled = false;
                _ctlScrollBarH.Maximum = 0;
                _ctlScrollBarH.ViewportSize = containerWidth;
            }

            if (contentHeight > containerHeight)
            {
                _ctlScrollBarV.Maximum = contentHeight - containerHeight;
                _ctlScrollBarV.IsEnabled = true;
                _ctlScrollBarV.ViewportSize = containerHeight;
            }
            else
            {
                _ctlScrollBarV.IsEnabled = false;
                _ctlScrollBarV.Maximum = 0;// containerHeight;
                _ctlScrollBarV.ViewportSize = containerHeight;
            }

            //设置标尺
            RuleX.Zoom = this._ctlMainFrame.ZoomFactor * 100;
            RuleY.Zoom = this._ctlMainFrame.ZoomFactor * 100;
            int temp = Convert.ToInt32(RuleX.Length / this._ctlMainFrame.ZoomFactor / 135);
            int interval = temp > 5 ? temp - (temp % 5) : 5;
            RuleX.Interval = interval;
            temp = Convert.ToInt32(RuleY.Length / this._ctlMainFrame.ZoomFactor / 110);
            interval = temp > 5 ? temp - (temp % 5) : 5;
            RuleY.Interval = interval;
        }

        void _ctlScrollBarH_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {
            double offsetX = this._ctlScrollBarH.Value;
            double offsetY = this._ctlScrollBarV.Value;
            _ctlMainFrame.MoveCameraToPos(offsetX, offsetY);

            RuleX.CountShift = (int)(offsetX);
            RuleY.CountShift = (int)(offsetY);

            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
              new Action(() =>
              {
                  //更新mask的位置
                  ShowDesignBorder(_selectedPart, _ctlCanvas);
              }));
        }

        void _ctlScrollBarV_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {
            double offsetX = this._ctlScrollBarH.Value;
            double offsetY = this._ctlScrollBarV.Value;
            _ctlMainFrame.MoveCameraToPos(offsetX, offsetY);
            RuleX.CountShift = (int)(offsetX);
            RuleY.CountShift = (int)(offsetY);
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
              new Action(() =>
              {
                  //更新mask的位置
                  ShowDesignBorder(_selectedPart, _ctlCanvas);
              }));
        }
        /// <summary>
        /// 将滚动条滚动到所选组件的位置
        /// </summary>
        /// <param name="selectedPart"></param>
        private void ScrollToSelected(Coolzon.UI.Part selectedPart)
        {
            if (selectedPart == null)
            {
                return;
            }
            Point point = selectedPart.XGetAbsolutePos();
            double hValue = point.X * _ctlMainFrame.ZoomFactor - ActualWidth / 2;
            double vValue = point.Y * _ctlMainFrame.ZoomFactor - ActualHeight / 2;


            this._ctlScrollBarH.Value = hValue / _ctlMainFrame.ZoomFactor;
            this._ctlScrollBarV.Value = vValue / _ctlMainFrame.ZoomFactor;

            RuleX.CountShift = (int)(this._ctlScrollBarH.Value / this._ctlMainFrame.ZoomFactor);
            RuleY.CountShift = (int)(this._ctlScrollBarH.Value / this._ctlMainFrame.ZoomFactor);
            _ctlMainFrame.MoveCameraToPos(this._ctlScrollBarH.Value, this._ctlScrollBarV.Value);
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
              new Action(() =>
              {
                  if (_ctlDesignBorder.Visibility == System.Windows.Visibility.Visible)
                  {
                      ShowDesignBorder(_selectedPart, this._ctlCanvas);
                      //保存设计边框的当前位置大小信息
                      this._dbPosStore = this._ctlDesignBorder.GetOffset();
                      this._dbSizeStore = this._ctlDesignBorder.GetSize();
                  }
              }));
        }
    }
}
